<template>
  <view class="container">
    <view class="header">
      <view class="back-arrow" @click="onBack" />
      <text class="title">
        快速还款
      </text>
      <view class="collection-text" @click="onCollectionDoc">
        <image src="@/static/images/pyq.png" class="pyq-icon" />
        <text>催收文案</text>
      </view>
    </view>

    <view class="search-bar">
      <input v-model="keyword" class="search-input" placeholder="搜索买家名称或手机号" confirm-type="search" @confirm="onSearch">
      <image src="@/static/images/search.png" class="search-icon" />
      <view class="filter" @click="toggleFilter">
        <image class="filter-icon" src="@/static/images/filter.png" />
        <span>筛选</span>
      </view>
    </view>

    <view class="summary">
      <view class="owe-count">
        总赊欠：
        <span class="owe-count-num">
          {{ formatNumber(totalOwe) }}</span>,
        共<span class="owe-count-num">{{ totalBuyers
        }}
        </span>人赊欠
      </view>
    </view>

    <scroll-view class="buyer-list" scroll-y @scrolltolower="onLoadMore" @pull-down-refresh="onPullDown">
      <view v-for="(buyer, index) in buyers" :key="index" class="buyer-item">
        <view class="list-item">
          <view>
            <view class="buyer-info">
              <text class="buyer-name">
                {{ buyer.name }}
              </text>
            </view>
            <view class="owe-info">
              <view>
                <text class="owe-label">
                  已赊欠：
                </text>
                <text class="owe-count-num">
                  {{ formatNumber(buyer.oweAmount) }}元
                </text>
              </view>
            </view>
          </view>
          <view class="repay-btn">
            还款
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
  import { ref } from 'vue';

  interface Buyer {
    name : string;
    oweAmount : number;
  }

  const keyword = ref('');
  const totalOwe = ref(643292);
  const totalBuyers = ref(39);
  const buyers = ref<Buyer[]>([
    {
      name: '临时买家',
      oweAmount: 75578,
    },
    {
      name: '临时买家',
      oweAmount: 75578,
    },
    {
      name: '临时买家',
      oweAmount: 75578,
    },
    {
      name: '临时买家',
      oweAmount: 75578,
    },
  ]);

  function onBack() {
    uni.navigateBack();
  }

  function onCollectionDoc() {
    console.log('催收文案');
  }

  function onSearch() {
    console.log('搜索:', keyword.value);
  }

  function toggleFilter() {
    console.log('切换筛选');
  }

  function formatNumber(num : number) {
    return num.toLocaleString();
  }

  function onPullDown() {
    console.log('下拉刷新');
  }

  function onLoadMore() {
    console.log('加载更多');
  }
</script>

<style scoped>
  .container {
    display: flex;
    flex-direction: column;
    background-color: #f8f8f8;
    min-height: 100vh;
  }

  .header {
    position: relative;
    display: flex;
    align-items: center;
    height: 88rpx;
    padding: 0 20rpx;
    background-color: #ffffff;
    border-bottom: 1rpx solid #f0f0f0;
  }

  .back-arrow {
    position: absolute;
    left: 20rpx;
    width: 30rpx;
    height: 30rpx;
    background: url("@/static/images/back.png") no-repeat center;
    background-size: contain;
  }

  .title {
    font-size: 36rpx;
    color: #333;
    font-weight: bold;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }

  .collection-text {
    position: absolute;
    right: 20rpx;
    font-size: 28rpx;
    color: #007aff;
    display: flex;
    align-items: center;
  }

  .pyq-icon {
    width: 32rpx;
    height: 32rpx;
    margin-right: 4rpx;
  }

  .search-bar {
    display: flex;
    align-items: center;
    height: 80rpx;
    padding: 0 20rpx;
    background-color: #ffffff;
    gap: 10%;
  }

  .search-input {
    flex: 1;
    font-size: 28rpx;
    color: #333;
    border-radius: 20px;
    padding: 10rpx 10rpx 10rpx 50rpx;
    background: rgba(245, 247, 250, 1);
    border: 1px solid rgba(229, 229, 229, 1);
    box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.25);
  }

  .search-icon {
    position: absolute;
    left: 28rpx;
    width: 40rpx;
    height: 40rpx;
  }

  .list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .filter {
    display: flex;
    width: 100rpx;
    height: 40rpx;
    font-weight: 500;
    font-size: 32rpx;
  }

  .filter span {
    color: #333;
    margin-left: 4rpx;
    line-height: 40rpx;
  }

  .filter-icon {
    width: 32rpx;
    height: 40rpx;

  }

  .summary {
    padding: 20rpx 40rpx;
    background-color: #ffffff;
  }

  .owe-count {
    font-size: 32rpx;
    color: rgba(128, 128, 128, 1);
    font-weight: 400;
  }

  .owe-count-num {
    color: #FF8D1A;
  }

  .buyer-list {
    flex: 1;
  }

  .buyer-item {
    background-color: #ffffff;
    padding: 24rpx;
    margin-bottom: 10rpx;
    border-radius: 8rpx;
    box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  }

  .buyer-info {
    margin-bottom: 8rpx;
  }

  .buyer-name {
    font-size: 32rpx;
    color: #333;
    font-weight: 600;
  }

  .owe-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .owe-label {
    font-size: 28rpx;
    color: #666;
  }

  .owe-amount {
    font-size: 32rpx;
    color: #ff6b01;
    flex: 1;
  }

  .repay-btn {
    background-color: #4cd964;
    color: #fff;
    font-size: 28rpx;
    padding: 4rpx 24rpx;
    border-radius: 4rpx;
    border: none;
    height: 56rpx;
    line-height: 56rpx;
    margin-right: 24rpx;
  }
</style>
